﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HR60_PanelSoft.View.TabForms.Handles;
using HR60_PanelSoft.Model;

namespace HR60_PanelSoft.View.TabForms.ListItems
{
    //GC: [g1].修改外部信号bug。[SceneLocalDeviceList]  @s3
    //GC: [g2].加载关联面板负载。[SceneLocalDeviceList] @s1
    //GC: [g3].保存对象到内存。[SceneLocalDeviceList] @s1
    //GC: [g4]bug2759[SceneLocalDeviceList]

	public partial class SceneLocalDeviceList : SceneDeviceList
	{
		public SceneLocalDeviceList()
		{
			InitializeComponent();
		}

		public void init(HR60_PanelSoft.Model.Panel panel)
		{
            refreshPinDevList(panel);

		}


        public  void refreshPinDevList(Model.Panel p)
        {
            // 负载设备列表
            this.tblList.Controls.Clear(); /* 先清理控件 */

            foreach (Pin pin in p.loadPins)
            {
                /* s1 鲁棒 */
                
                if (null == pin.connectedDevice || pin.connectedDevice == string.Empty)
                    continue;

                CatchValueChange = false;
                /* end of s1 */
                /* s2 验证:屏蔽isMainPin */
                if (findDeviceByUuid(pin.connectedDevice).type.canButtonCtrl)
                /* end of s2 */

                //if (pin.isMainPin && findDeviceByUuid(pin.connectedDevice).type.canButtonCtrl)
                {
                    Device dev = findDeviceByUuid(pin.connectedDevice);
                    SceneDeviceItem item = new SceneDeviceItem(dev);
                    /* [g4.s1]bug2759 此处有错 */
                    //if (p.type.hasSignalModel && p.signal.scene != null)
                    if(p.type.hasSignalModel && p.signal.localScene != null)
                    /* end of [g4.s1] */
                    {
                        /* g2.s1 */
                        //Scene scene = findSceneByUuid(p.signal.scene);
                        //SceneEvent se = scene.findEventByDevice(dev.uuid);
                        //item.Value = se;
                        Scene lscene = p.signal.localScene;
                        SceneEvent se = null;
                        if(null != lscene)
                            se = lscene.findEventByDevice(dev.uuid);
                        if (null != se)
                            item.LoadValue(se);
                        else
                            item.Value = se;
                        /* end of g2.s1 */
                        /* g1.s3 将valueChange传递过去*/
                        //item.addDropDownEvent(valueChange);
                        /* end of g1.s3 */
                    }
                    this.tblList.Controls.Add(item);
                }

                CatchValueChange = true;
            }
        }

        /* g3.s1 重写Value，返回类型为Scene */
        /// <summary>
        /// 返回Scene类型。Value只有获取功能，设置使用refreshPinDevList。
        /// </summary>
        public override object Value
        {
            get
            {
                Scene rst = new Scene();

                foreach (Control c in tblList.Controls)
                {
                    SceneDeviceItem sdi = (SceneDeviceItem)c;
                    SceneEvent se = new SceneEvent();
                    se = (SceneEvent)sdi.Value;
                    if(null != se)
                        rst.events.Add(se);
                }
                if (rst.events.Count < 1)
                    return null;
                return rst;
            }
        }

        /* g3.s1 */

	}
}
